var currentFileName = "";
var loadFile = function(fileName) {
    $("#content").val('Loading content...')
    $("#content").attr('disabled', 'disabled');
    var request = $.ajax({
                url:"/f/" + fileName,
                type:"GET",
                success:function() {
                    currentFileName = fileName;
                    $("#content").removeAttr('disabled');
                    $("#content").val(request.responseText);
                    $("#message").html("File loaded!");
                }
            });
}
var saveFile = function() {
    if (currentFileName.trim().length > 0) {
        var userName = $("#user").val();
        var password = $("#pass").val();
        var content = $("#content").val();
        var request = $.ajax({
                    url:"/f/" + currentFileName,
                    type:"POST",
                    data:{
                        user:userName,
                        pass:password,
                        action:"save",
                        content:content
                    },
                    success:function() {
                        $("#message").html(request.responseText);
                    }
                });
    }
}

var parseFile = function() {
    if (currentFileName.trim().length > 0) {
        var userName = $("#user").val();
        var password = $("#pass").val();
        var request = $.ajax({
                    url:"/rest/prolog/parse/" + currentFileName,
                    type:"POST",
                    data:{
                        user:userName,
                        pass:password
                    },
                    success:function() {
                        $("#message").html(request.responseText);
                    }
                });
    }
}

var parseAll = function() {
    var files = ['laptop.txt','rule.txt','question.txt'];
    var userName = $("#user").val();
    var password = $("#pass").val();
    for (var i = 0; i < files.length; i++) {
        var request = $.ajax({
                    url:"/rest/prolog/parse/" + files[i],
                    type:"POST",
                    data:{
                        user:userName,
                        pass:password
                    },
                    success:function() {
                        $("#message").html(request.responseText);
                    }
                });
    }
}

var clearAll = function() {
    var userName = $("#user").val();
    var password = $("#pass").val();
    var request = $.ajax({
                url:"/rest/prolog/content/reset",
                type:"POST",
                data:{
                    user:userName,
                    pass:password
                },
                success:function() {
                    $("#message").html(request.responseText);
                }
            });

}